// carditem.cpp
#include "carditem.h"
#include <QGraphicsSceneHoverEvent>

CardItem::CardItem(const QPixmap& pixmap,int a,int n, QGraphicsItem* parent)
    : QGraphicsObject(parent),m_pixmap(pixmap),ending(a),n(n)
{
    setFlag(QGraphicsItem::ItemIsMovable);
    setFlag(QGraphicsItem::ItemIsSelectable);
    setFlag(QGraphicsItem::ItemSendsScenePositionChanges);

    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &CardItem::checkForCollision);
}

void CardItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        m_isDragging = false;
        m_startPos = pos();
        m_itemToSwapWith = nullptr;
        m_timer->start(50);
    }
    QGraphicsObject::mousePressEvent(event);
}

void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
    m_isDragging = true;
    QGraphicsObject::mouseMoveEvent(event);
    checkForCollision();
}

void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
    m_timer->stop();

    if (!m_isDragging)
    {
        toggleLift();
        emit clicked();
        return;
    }

    QPropertyAnimation* animation = new QPropertyAnimation(this, "pos");
    animation->setDuration(200);

    if (m_itemToSwapWith) {
        bool thisLifted = m_isLifted;
        bool otherLifted = m_itemToSwapWith->m_isLifted;
        animation->setStartValue(pos());
        animation->setEndValue(m_itemToSwapWith->pos());

        QPropertyAnimation* otherAnim = new QPropertyAnimation(m_itemToSwapWith, "pos");
        otherAnim->setDuration(200);
        otherAnim->setStartValue(m_itemToSwapWith->pos());
        otherAnim->setEndValue(m_startPos);

        animation->start(QAbstractAnimation::DeleteWhenStopped);
        otherAnim->start(QAbstractAnimation::DeleteWhenStopped);

        m_isLifted = otherLifted;
        m_itemToSwapWith->m_isLifted = thisLifted;

        // Adjust Y position if necessary
        if (m_isLifted)
            setY(y() - 40);
        else
            setY(y() + 40);

        if (m_itemToSwapWith->m_isLifted)
            m_itemToSwapWith->setY(m_itemToSwapWith->y() - 40);
        else
            m_itemToSwapWith->setY(m_itemToSwapWith->y() + 40);
    }

    else
    {
        animation->setStartValue(pos());
        animation->setEndValue(m_startPos);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
    }

    QGraphicsObject::mouseReleaseEvent(event);
}

void CardItem::checkForCollision()
{
    m_itemToSwapWith = nullptr;
    QList<QGraphicsItem*> colliding = collidingItems();
    for (QGraphicsItem* item : colliding)
    {
        CardItem* other = dynamic_cast<CardItem*>(item);
        if (other && other != this)
        {
            m_itemToSwapWith = other;
            break;
        }
    }
}
void CardItem::toggleLift() {
    QPropertyAnimation* liftAnim = new QPropertyAnimation(this, "pos");
    liftAnim->setDuration(200);
    QPointF currentPos = pos();

    if (!m_isLifted)
    {
        liftAnim->setStartValue(currentPos);
        liftAnim->setEndValue(currentPos + QPointF(0, -40));
    }
    else
    {
        liftAnim->setStartValue(currentPos);
        liftAnim->setEndValue(currentPos + QPointF(0, 40));
    }
    liftAnim->start(QAbstractAnimation::DeleteWhenStopped);
    m_isLifted = !m_isLifted;
}
